package io.github.tangwan.utils;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

/**
 * @author tangwan
 * @version V1.0
 * created on 2021/5/28 10:41
 * @since JDK 1.8
 */
public abstract class DateUtil {

    /**
     * JDK8 Date ==> LocalDate
     * @param date
     * @return
     */
    public static LocalDate dateToLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.toLocalDate();
    }

    /**
     * JDK8 Date ==> LocalDateTime
     * @param date
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    /**
     * 获取指定某天的开始时间
     * @param date
     * @return
     */
    public static Date getStartTime(Date date) {
        Calendar dateStart = Calendar.getInstance();
        dateStart.setTime(date);
        dateStart.set(Calendar.HOUR_OF_DAY, 0);
        dateStart.set(Calendar.MINUTE, 0);
        dateStart.set(Calendar.SECOND, 0);
        dateStart.set(Calendar.MILLISECOND,0);
        return dateStart.getTime();
    }

    /**
     * 日期加减
     * @param date
     * @param y
     * @param month
     * @param d
     * @param h
     * @param m
     * @param s
     * @return
     */
    public static Date dateChange(Date date, int y, int month, int d, int h, int m, int s) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.YEAR, y);
        rightNow.add(Calendar.MONTH, month);
        rightNow.add(Calendar.DAY_OF_YEAR, d);
        rightNow.add(Calendar.HOUR_OF_DAY, h);
        rightNow.add(Calendar.MINUTE, m);
        rightNow.add(Calendar.SECOND, s);
        return rightNow.getTime();
    }

}
